#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.data) #回调函数 收到的参数.data是通信的数据 默认通过这样的 def callback(data) 取出data.data数据

def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # name are launched, the previous one is kicked off. The
    # anonymous=True flag means that rospy will choose a unique
    # name for our 'listener' node so that multiple listeners can
    # run simultaneously.
    rospy.init_node('listener', anonymous=True)
      #启动节点并同时为节点命名 

    rospy.Subscriber('chatter', String, callback)
     #启动订阅，订阅主题‘chatter’，及标准字符串格式，同时调用回调函数，当有数据时调用函数，取出数据

    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

if __name__ == '__main__':
    listener()


